﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;

public partial class login : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand comm;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = DBTools.getDBConnection();
        comm = new SqlCommand("",conn);
    }
    //重置按钮点击事件
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        //验证用户是否存在
        int usercheck = checkUserName();
        if (usercheck == 0)
        {
            Label3.Text = "用户不存在！";
            Label3.ForeColor = System.Drawing.Color.Red;
        }
        else if (usercheck == 1)
        {
            Label3.Text = "";
        }
        else
        {
            Label3.Text = "账户异常";
            Label3.ForeColor = System.Drawing.Color.Red;
        }
        //验证用户名密码是正确
        if (usercheck == 1)//如果用户存在
        {
            Int64 userID = checkLogin();
            if (userID != 0)
            {
                Label4.Text = "登录成功！";
                Label4.ForeColor = System.Drawing.Color.Green;
                //写入session
                Session.Add("userID",userID);
                Session.Add("userCode", TextBox1.Text.Trim());
                //页面跳转
                Response.Redirect("index.aspx");
            }
            else
            {
                Label4.Text = "用户名或密码错误！";
                Label4.ForeColor = System.Drawing.Color.Red;
            }
      
        }
    }
    //验证提示用户名是否存在
    private int checkUserName()
    {
        comm.CommandText = "select COUNT(*) from SH_YXK_User where Code = @loginame";
        comm.Parameters.Add(new SqlParameter("@loginame", SqlDbType.NVarChar, 50));
        comm.Parameters["@loginame"].Value = TextBox1.Text.Trim();
        comm.Connection.Open();
        int mark = (int)comm.ExecuteScalar();
        comm.Connection.Close();
        comm.CommandText = "";
        return mark;
    }
    //验证用户名密码
    private Int64 checkLogin()
    {
        comm.CommandText = "select count(*) from base_user where Code = @loginame and [Password] = @U9loginPwd";
        comm.Parameters.Add(new SqlParameter("@U9loginPwd", SqlDbType.NVarChar, 150));
        comm.Parameters["@U9loginPwd"].Value = Encrypt(TextBox2.Text.Trim());
        comm.Connection.Open();
        int aa = int.Parse(comm.ExecuteScalar().ToString());
        comm.Connection.Close();
        Int64 userID = 0;
        if (aa != 0)
        {
            comm.CommandText = "select ID from SH_YXK_User where Code = @loginame";
            comm.Connection.Open();
            userID = (Int64)comm.ExecuteScalar();
            comm.Connection.Close();
            comm.CommandText = "";
        }
        return userID;
    }
    //Md5加密字符串
    private string Encrypt(string source)
    {
        MD5 md = MD5.Create();
        byte[] bytes = new UnicodeEncoding().GetBytes(source);
        return Convert.ToBase64String(md.ComputeHash(bytes));
    }
}